package cn.tedu.medicalmanagementsystem.manager.service.impl;

import cn.tedu.medicalmanagementsystem.base.exception.ServiceException;
import cn.tedu.medicalmanagementsystem.base.response.StatusCode;
import cn.tedu.medicalmanagementsystem.manager.mapper.ManagerEscortMapper;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.EscortAddParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.EscortListQuery;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.EscortUpdateParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.entity.Escort;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.EscortVO;
import cn.tedu.medicalmanagementsystem.manager.service.ManagerEscortService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ManagerEscortServiceImpl implements ManagerEscortService {
    @Autowired
    ManagerEscortMapper managerEscortMapper;

    @Override
    public EscortVO add(EscortAddParam escortAddParam) {
        log.debug("开始处理添加陪诊员业务，参数: {}", escortAddParam);
        //检查陪诊员是否存在
        EscortVO escortid = managerEscortMapper.selectById(escortAddParam.getId());
        if (escortid != null) {
            throw new ServiceException(StatusCode.OPERATION_FAIL);
        }
        Escort escort = new Escort();
        BeanUtils.copyProperties(escortAddParam, escort);
        escort.setPassword("123456");
        escort.setCreateTime(LocalDate.now());
        escort.setAvgRating(5.0);
        managerEscortMapper.add(escort);
        return managerEscortMapper.selectById(escort.getId());
    }

    //  更新陪诊员信息
    @Override
    public EscortVO update(@Valid EscortUpdateParam escortUpdateParam) {
        log.debug("开始处理更新陪诊员业务，参数: {}", escortUpdateParam);

        // 查询当前要更新的记录
        EscortVO escortInDB = managerEscortMapper.selectById(escortUpdateParam.getId());
        System.out.println("查询到的用户数据使：+++++++++++++++++++++"+escortInDB);
        if (escortInDB == null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
        // 如果 idCard 被修改，则检查是否重复
        if (!escortInDB.getIdCard().equals(escortUpdateParam.getIdCard())) {
            Escort existingEscort = managerEscortMapper.selectByIdCard(escortUpdateParam.getIdCard());
            if (existingEscort != null) {
                throw new ServiceException(StatusCode.USERNAME_EXISTS);
            }
        }
        Escort escort = new Escort();
        BeanUtils.copyProperties(escortUpdateParam, escort);
        System.out.println("转化后的用户是：++++++++++++++++++++++"+escort);
        managerEscortMapper.update(escort);
        return managerEscortMapper.selectById(escort.getId());
    }

//    //  删除陪诊员
//    @Override
//    public int delete(EscortDelParam escortDelParam) {
//        log.debug("开始处理删除陪诊员业务，参数: {}", escortDelParam);
//        managerEscortMapper.deleteById(escortDelParam.getId());
//        return 1;
//    }

    @Override
    public List<EscortVO> allEscorts() {
        return managerEscortMapper.allEscorts();
    }

    @Override
    public EscortVO getById(Long id) {
        return managerEscortMapper.selectById(id);
    }

    @Override
    public EscortVO getByUsername(EscortVO escortVO) {
        return managerEscortMapper.selectByUsername(escortVO);
    }

    @Override
    public List<EscortVO> selectEscort(EscortListQuery escortListQuery) {
        log.debug("查询陪诊员业务:escortQuery={}",escortListQuery);
        List<EscortVO> list = managerEscortMapper.selectEscort(escortListQuery);
        return list;
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除陪诊员业务，参数: {}", id);
        // 检查陪诊员是否存在
        EscortVO existingEscort = managerEscortMapper.selectById(id);
        if (existingEscort == null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
        managerEscortMapper.deleteById(id);
    }


}
